package com.axios.utils;

import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

public class DBHelper {
    // 数据库的连接
    public static Connection conn;
    // 连接数据库的信息
    private static String url;
    private static String username;
    private static String password;

    static{
        try{
            // 读取配置文件信息
            Properties prop = new Properties();
            InputStream input = DBHelper.class.getClassLoader().getResourceAsStream("db.properties");
            prop.load(input);

            // 从属性中获取连接数据库详情
            String driver = prop.getProperty("db.driver");
            url = prop.getProperty("db.url");
            username = prop.getProperty("db.username");
            password = prop.getProperty("db.password");

            // 加载驱动
            Class.forName(driver);
        }catch (IOException e) {
            throw new RuntimeException(e);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }

    }

    /**
     * 开启数据库连接
     * */
    public static Connection getConn(){
        try {
            conn = DriverManager.getConnection(url,username,password);
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("开启数据库连接失败！");
        }
        return conn;
    }

    /**
     * 返回结果集
     * @param sql SQL语句
     * @param objs 要注入的参数数组
     * @return
     */
    public static ResultSet executeQuery(String sql, Object[] objs){
        ResultSet rs = null;
        try{
            // 判断是否要注入参数
            if(objs == null){
                Statement statement = conn.createStatement();
                rs = statement.executeQuery(sql);
            }else{
                PreparedStatement pstm = conn.prepareStatement(sql);
                // 注入参数
                for(int i = 0; i < objs.length; i++){
                    pstm.setObject(i + 1, objs[i]);
                }
                rs = pstm.executeQuery();
            }
        }catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return rs;
    }

    /**
     * 返回受影响的行数
     * @param sql SQL语句
     * @param objs 要注入的参数数组
     * @return
     */
    public static Integer executeUpdate(String sql, Object[] objs){
        Integer count = -1;
        try{
            // 判断是否要注入参数
            if(objs == null){
                Statement statement = conn.createStatement();
                count = statement.executeUpdate(sql);
            }else{
                PreparedStatement pstm = conn.prepareStatement(sql);
                // 注入参数
                for(int i = 0; i < objs.length; i++){
                    pstm.setObject(i + 1, objs[i]);
                }
                count = pstm.executeUpdate();
            }
        }catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return count;
    }

    /**
     * 关闭数据库连接
     * */
    public static void closeConn(){
        try {
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("关闭数据库连接失败！");
        }
    }
}
